Dynamic candlestick chart from WAEX
Hard dev initialization
PacletDirectoryLoad[FileNameJoin[{$HomeDirectory, "Projects", "Objects"}]];
PacletDirectoryLoad[FileNameJoin[{$HomeDirectory, "Projects", "WAEXLink"}]];
PacletDirectoryLoad[FileNameJoin[{$HomeDirectory, "Projects", "CSockets"}]];
PacletDirectoryLoad[FileNameJoin[{$HomeDirectory, "Projects", "LTP"}]];
PacletDirectoryLoad[FileNameJoin[{$HomeDirectory, "Projects", "SocketIOLink"}]];
Block[{$ContextPath},
Get[FileNameJoin[{$HomeDirectory, "Projects", "CSockets", "Kernel", "TCP.wl"}]];
Get[FileNameJoin[{$HomeDirectory, "Projects", "CSockets", "Kernel", "Handler.wl"}]];
Get[FileNameJoin[{$HomeDirectory, "Projects", "LTP", "Kernel", "LTP.wl"}]];
Get[FileNameJoin[{$HomeDirectory, "Projects", "SocketIOLink", "Kernel", "SocketIOLink.wl"}]];
Get[FileNameJoin[{$HomeDirectory, "Projects", "WAEXLink", "Kernel", "RealTime.wl"}]];
];
Create connection
Once[connection = KirillBelov`WAEXLink`RealTime`WAEXConnect[
"kirillbelovtest@gmail.com",
"Wolfr4mCCDBK1r1!",
"MessageHandler" -> eventHandler
]];
Event handler that can update the chart
eventHandler = Function[
Module[{newData},
Print[event = #];
newData =
Map[List] @
Normal @
Transpose @
Dataset @
Query[All, <|
"x" -> 1 /* (#/1000&) /* FromUnixTime /* (DateString[#, "ISODateTime"]&),
"open" -> 2, "high" -> 3, "low" -> 4, "close" -> 5
|>] @ #[["data", 2, "payload", "charts"]];
];
PlotlyExtendTraces[candlestickChart, newData, {0}];
];
Subscribe to candlestick events
Once[KirillBelov`WAEXLink`RealTime`WAEXSubscribeToPricing[connection, "CandleStick", 1]];
Init data. **WAIT WHEN FIRST DATA WILL BE RECEIVED!**
initData =
Normal @
Transpose @
Dataset @
Flatten @
DeleteDuplicates @
Query[All, "data", 2, "payload", "charts", All, <|
"x" -> 1 /* (#/1000&) /* FromUnixTime /* (DateString[#, "ISODateTime"]&),
"open" -> 2, "high" -> 3, "low" -> 4, "close" -> 5
|>] @
Query[Select[#data[[1]] == "Pricing_CandleStick_1"&]] @ connection["Data"];
Dynamic chart
candlestickChart = Plotly[<|
"type" -> "candlestick"
|> ~ Join ~ initData, <|
"width" -> 1000,
"height" -> 600,
"paper_bgcolor" -> "rgba(0, 0, 0, 0)",
"plot_bgcolor" -> "rgba(0, 0, 0, 0)",
"margin" -> "autoexpand"
|>]
Import[FileNameJoin[{"attachments", "image-4ab.png"}]]